package com.crm.business.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.crm.business.admin.mapper.AdminUserManageDeptMapper;
import com.crm.business.admin.service.IAdminDeptService;
import com.crm.business.admin.service.IAdminUserManageDeptService;
import com.crm.business.admin.service.IAdminUserService;
import com.crm.business.servlet.BaseServiceImpl;
import com.crm.business.utils.UserUtil;
import com.crm.common.admin.common.AdminConst;
import com.crm.common.admin.entity.PO.AdminDept;
import com.crm.common.admin.entity.PO.AdminRole;
import com.crm.common.admin.entity.PO.AdminUserManageDept;
import com.crm.common.admin.entity.PO.AdminUserRole;
import com.crm.common.admin.entity.VO.AdminUserVO;
import com.crm.common.core.redis.RedisCache;
import com.crm.common.ka.core.common.Const;
import com.crm.common.ka.core.feign.admin.entity.DataTypeEnum;
import com.crm.common.utils.spring.SpringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class AdminUserManageDeptServiceImpl extends BaseServiceImpl<AdminUserManageDeptMapper, AdminUserManageDept> implements IAdminUserManageDeptService {

    @Autowired
    private IAdminDeptService adminDeptService;
    @Autowired
    private RedisCache redisCache;

    /**
     * 管理部门
     *
     * @param userId   用户ID
     * @param isRemove 是否删除原有部门
     * @param deptIds   部门列表
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveByUserId(Long userId, boolean isRemove, List<String> deptIds) {
        if (isRemove) {
            QueryWrapper<AdminUserManageDept> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("user_id", userId);
            remove(queryWrapper);
        }
        List<AdminUserManageDept> adminUserManageDeptList = new ArrayList<>();
        for (String deptId : deptIds) {
            adminUserManageDeptList.add(new AdminUserManageDept().setUserId(userId).setDeptId(Integer.parseInt(deptId)));
        }
        saveBatch(adminUserManageDeptList, Const.BATCH_SAVE_SIZE);
    }


    /**
     * 通过用户ID查询管理部门
     *
     * @param userId 用户ID
     * @return data
     */
    @Override
    public List<AdminDept> queryManageDeptListByUserId(Long userId) {
        QueryWrapper<AdminUserManageDept> wrapper = new QueryWrapper<>();
        wrapper.select("dept_id");
        wrapper.eq("user_id", userId);
        List<Integer> deptIdList = list(wrapper).stream().map(AdminUserManageDept::getDeptId).collect(Collectors.toList());
        if (deptIdList.size() > 0) {
            return adminDeptService.listByIds(deptIdList);
        }
        return new ArrayList<>();
    }

    /**
     * 管理部门查询用户
     * @return
     */
    @Override
    public List<Long> queryUserIdsByManageDept(){
        QueryWrapper<AdminUserManageDept> wrapper = new QueryWrapper<>();
        wrapper.select("dept_id");
        wrapper.eq("user_id", UserUtil.getUserId());
        List<Integer> deptIdList = list(wrapper).stream().map(AdminUserManageDept::getDeptId).collect(Collectors.toList());
        List<Long> userIds = new ArrayList<>();
        List<Integer> allDeptIds = new ArrayList<>();
        for(Integer deptId : deptIdList){
            allDeptIds.addAll(SpringUtils.getBean(IAdminDeptService.class).queryChildDept(deptId));
            allDeptIds.add(deptId);
        }
//        userIds.addAll(SpringUtils.getBean(IAdminUserService.class).queryUserByDeptIds(allDeptIds));
        for(Integer deptId : allDeptIds){
            List<Long> cacheUserIds = redisCache.getCacheMapValue(AdminConst.USER_DEPT_ID_CACHE_,String.valueOf(deptId));
            if(!CollectionUtils.isEmpty(cacheUserIds)){
                userIds.addAll(cacheUserIds);
            }
        }
        userIds = userIds.stream().distinct().collect(Collectors.toList());
        return userIds;
    }
}
